#include "Multicast/Multicast.h"


int main(int argc, char** argv)
{
    int sendfd, recvfd;
    const int on = 1;
    struct sockaddr *sendaddr, *recvaddr;
    socklen_t salen = sizeof(struct sockaddr);

    if (argc != 3)
    {
        err_quit("usage: %s <IP-multicast-address> <port#>", *argv);
    }

    sendfd = udp_client(argv[1], argv[2], (void**)&sendaddr, &salen);
    mcast_setloop(sendfd, 0);//禁用本地回环

    recvfd = socket(sendaddr->sa_family, SOCK_DGRAM, 0);
    setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    recvaddr = (struct sockaddr*)malloc(salen);
    memcpy(recvaddr, sendaddr, salen);
    display_sockaddr(recvaddr);
    bind(recvfd, recvaddr, salen);
    int RS = mcast_join(recvfd, sendaddr, salen, "enp2s0", 0);//加入多播组
    if (RS < 0)
    {
        printf("errno :%d\tmsg :%s\n", errno, strerror(errno));
        return RS;
    }
    
    if (fork() == 0)
    {
        recv_all(recvfd, salen);
    }
    send_all(sendfd, sendaddr, salen);
}